<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%--<head>
    <meta charset="UTF-8">
    <title>档案绑定</title>
    &lt;%&ndash; <c:import url="/WEB-INF/jsp/header.jsp"/>&ndash;%&gt;
    <style type="text/css">
        .form-like th {
            font-weight: normal;
            text-align: right;
        }

        body {
            padding: 10px 10px 10px 50px;
        }

        textarea {
            height: auto;
        }

        table {
            margin-bottom: 10px;
        }
    </style>
</head>--%>
<script type="text/javascript">
    var recFilesIds = "${recFilesIds}";
</script>
<body>
<div id="valueHolder">
    <input type="hidden" name="orderId" value="${orderId}">
    <input type="hidden" name="orderNo" value="${orderNo}">
</div>
<div class="body-main">

    <div class="titleSearch">
        <table class="form-like">
            <%--<tr>
                <th width="120">生产订单编号：</th>
                <td>${orderNo}</td>
            </tr>--%>
            <tr>
                <th>当前绑定档案编号：</th>
                <td id="recFilesNos">${recFilesNos}</td>
            </tr>
            <tr>
                <th>绑定档案：</th>
                <td><input type="text" class="input-style" name="khOrDaStr" placeholder="请输入客户名/客户编号/档案编号"
                <%--value="KH2018060900003"--%>
                           style="width: 200px;">
                    <div class="layui-input-inline">
                        <button class="layui-btn layui-btn-mini layui-btn-normal" onclick="searchData()"
                                style="margin-left:30px;">搜索
                        </button>
                        <%--<button class="layui-btn layui-btn-mini layui-btn-primary" onclick="getData()"--%>
                        <%--style="margin-left: 20px;">绑定--%>
                        <%--</button>--%>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div>
        <table id="filesTable" class="cell-border crm-table columnsHidden dataTable">
            <colgroup>
                <%--<col width="20">--%>
                <col width="50">
                <col>
                <col>
                <col>
                <col>
                <col>
                <col>
                <col width="80">
            </colgroup>
        </table>
    </div>


</div>

<%--<c:import url="/WEB-INF/jsp/footer.jsp"/>--%>
<script src="/static/js/choose.extend.js?v=${version}" type="text/javascript"></script>
<script src="${basePath}/static/js/zzsc/production/sc_common.js?v=${version}" type="text/javascript"
        charset="utf-8"></script>
<script type="text/javascript">
    var table;

    function searchData() {
        /* if (table != undefined) {
             table.ajax.reload();
             return;
         }*/
        var khOrDaStr = $(".titleSearch input[name='khOrDaStr']").val().trim();
        if (khOrDaStr == "") {
            DGG.Warning("搜索条件不能为空！", {icon: 0});
            return;
        }
        var url = "/zzsc/commonLayer/queryFiles.do";
        table = $('#filesTable').dataTableExtend({
            "ajax": {"url": url, "type": "post"},
            "pageLength": 10,
            "fnServerParams": function (aoData) {
                aoData.khOrDaStr = $(".titleSearch input[name='khOrDaStr']").val().trim();
                return aoData;
            },
            "scrollX": "true",//是否开启横项滚动
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 0,//左侧锁定2列
                "rightColumns": 0//右侧锁定1列
            },
            "aoColumns": [

                {
                    "title": "序号",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return meta.row + 1;
                    }
                }, {
                    "title": "客户名",
                    "orderable": false,
                    "data": "filesCustomerName"
                },
                {
                    "title": "客户编号",
                    "orderable": false,
                    "data": "filesCustomerNo"
                },
                {
                    "title": "档案编号",
                    "orderable": false,
                    "data": "filesNo"
                }
                , {
                    "title": "档案位置",
                    "orderable": false,
                    "data": "filesLocation"
                }
                , {
                    "title": "档案状态",
                    "orderable": false,
                    "data": "filesStatus",
                    "render": function (data) {
                        var str = "";
                        if (data == 1) {
                            str = "在库";
                        } else if (data == 2) {
                            str = "出库";
                        }
                        else if (data == 3) {
                            str = "借出";
                        }
                        return str;

                    }
                },
                {
                    "title": "绑定状态",
                    "orderable": false,
                    "data": "filesId",
                    "render": function (data) {
                        var str = recFilesIds.indexOf(data) > -1 ? "已绑定" : "未绑定";
                        return str;
                    }
                }, {
                    "title": "操作",
                    "orderable": false,
                    "data": "filesId",
                    "render": function (data) {
                        var str = recFilesIds.indexOf(data) > -1 ? "解除绑定" : "绑定";
                        return "<a href='javascript:void(0);' class='layui-table-link' onclick='operateRecord()'>" + str + "</a>";
                    }
                }],
            "columnDefs": [
                {
                    targets: ["_all"], "defaultContent": ""
                }
            ]
        });
    }

    function operateRecord() {
        var $a = $(event.target);
        var operate = $a.text();
        var rowIndex = $a.parents("tr").eq(0).find("td").eq(0).text();
        var datas = $('#filesTable').dataTableExtend("getData");
        var files = datas[rowIndex - 1];
        var url = "/zzsc/commonLayer/bindOrUnbindFiles.do";

        var para = $("#valueHolder").serialize2object();
        $.extend(para, files, {operate: operate});
        DGG.Layer.loading.start();
        $.ajax({
            url: url,
            type: "POST",
            data: para,
            success: function (data) {
                DGG.Layer.loading.done();
                var msg = data.msg;
                if (msg.indexOf("成功") > -1) {
                    var newOperate = "绑定";
                    var newStatus = "未绑定";
                    var recFilesNos = $("#recFilesNos").text();
                    //刷新页面数据
                    if (operate == "绑定") {
                        newOperate = "解除绑定";
                        newStatus = "已绑定";
                        recFilesNos = addItem(recFilesNos, files.filesNo, " , ", "暂未关联");
                        recFilesIds = addItem(recFilesIds, files.filesId, ",", "");
                    } else {
                        recFilesNos = removeItem(recFilesNos, files.filesNo, " , ", "暂未关联");
                        recFilesIds = removeItem(recFilesIds, files.filesId, ",", "");
                    }

                    $("#recFilesNos").html(recFilesNos);
                    $a.html(newOperate);
                    $a.parents("tr").eq(0).find("td").eq(6).text(newStatus);
                    //
                    DGG.Succeed(operate + "成功！");
                } else {
                    DGG.Errors(operate + "失败！");
                }
            }
        });

    }

    function closeThisLayer() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    function addItem(str, item, sep, empty) {
        if (str.trim() == empty) str = "";

        str = str.trim();
        item = item.trim();
        if (str != "") {
            str = str + sep + item;
        } else {
            str = item;
        }

        return str;
    }

    function removeItem(str, item, sep, empty) {
        str = str.trim();
        item = item.trim();

        str = str.replace(sep + item, "");
        str = str.replace(item + sep, "");
        str = str.replace(item, "");

        if (str.trim() == "") str = empty;
        return str;
    }
</script>
